package com.ihrm.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ihrm.common.utils.IdWorker;
import com.ihrm.domain.company.Department;
import com.ihrm.domain.system.*;
import com.ihrm.system.client.DepartmentFeignClient;
import com.ihrm.system.mapper.RoleMapper;
import com.ihrm.system.mapper.UserMapper;
import com.ihrm.system.mapper.UserRoleMapper;
import com.ihrm.system.service.api.UserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.Date;
import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private DepartmentFeignClient departmentFeignClient;

    @Autowired
    private IdWorker idWorker;





    @Override
    public void add(User user) {
        String id = idWorker.nextId() + "";
        user.setId(id);
        String password = new Md5Hash("123456", user.getMobile(), 3).toString();
        //设置初始密码
        user.setPassword(password);
        //设置启用状态
        user.setEnableState(1);
        user.setLevel("user");
        user.setInServiceStatus(1);
        user.setCreateTime(new Date());
        userMapper.insertSelective(user);
    }

    @Override
    public void update(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public void deleteById(String id) {
        userMapper.deleteByPrimaryKey(id);

    }

    @Override
    public User findById(String id) {
        return userMapper.selectUserAndRoleByPrimaryKey(id);
    }

    @Override
    public PageInfo<User> getUserListPageInfo(String companyId, String departmentId, int page, int size) {
        // 1.开启分页功能
        PageHelper.startPage(page, size);
        List<User> userList = userMapper.getUserList(companyId, departmentId);
        // 3.为了方便页面使用将 userList 封装为
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        return pageInfo;
    }

    @Override
    public void assignRoles(String userId, List<String> roleIds) {
        //根据id查询用户
        User user = userMapper.selectUserAndRoleByPrimaryKey(userId);

        UserRoleExample userRoleExample = new UserRoleExample();
        UserRoleExample.Criteria criteria = userRoleExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        userRoleMapper.deleteByExample(userRoleExample);

        //设置用户的角色
        for (String roleId : roleIds) {
            UserRoleKey userRoleKey = new UserRoleKey(roleId, userId);
            userRoleMapper.insert(userRoleKey);
        }
    }

    @Override
    public User findByMobile(String mobile) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andMobileEqualTo(mobile);
        return userMapper.selectByExample(userExample);
    }


    @Override
    public User selectUserAndRoleByPrimaryKey(String id) {
        return userMapper.selectUserAndRoleByPrimaryKey(id);
    }

    @Override
    public List<User> findAll(String companyId) {
        return userMapper.findAll(companyId);
    }

    //批量添加
    @Transactional
    @Override
    public void saveAll(List<User> list, String companyId, String companyName) {
        for (User user : list) {
            //设置初始密码
            String password = new Md5Hash("123456", user.getMobile(), 3).toString();
            user.setPassword(password);
            //id
            String id = idWorker.nextId() + "";
            user.setId(id);
            //基本属性
            user.setCompanyId(companyId);
            user.setCompanyName(companyName);
            user.setEnableState(1);
            user.setInServiceStatus(1);
            user.setLevel("user");
            user.setCreateTime(new Date());
            Department department = departmentFeignClient.findByCode(user.getDepartmentId(), companyId);
            if(department!=null){
                user.setDepartmentId(department.getId());
                user.setDepartmentName(department.getName());
            }
            userMapper.insertSelective(user);
        }
    }
}
